﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMA = System.Management.Automation;

namespace RSPowershell
{
    public static class PrintSystem
    {
        public static System.Drawing.Printing.PrinterSettings GetPrinterSettings(SMA.Cmdlet cmdlet, string printername)
        {
            cmdlet.WriteVerbose("Getting Printer Settings");
            var print_settings = new System.Drawing.Printing.PrinterSettings();

            if (printername != null)
            {
                cmdlet.WriteVerbose("Setting settings to use printer \"{0}\"", printername);
                print_settings.PrinterName = printername;
            }
            else
            {
                cmdlet.WriteVerbose("Using settings for default printer {0}", print_settings.PrinterName);
            }

            return print_settings;

        }

        private static Dictionary<string, IList<System.Drawing.Printing.PaperSource>> paper_source_cache;
        private static Dictionary<string, IList<System.Drawing.Printing.PaperSize>> paper_size_cache;

        public static IList<System.Drawing.Printing.PaperSource> GetPaperSources(SMA.Cmdlet cmdlet, string printername)
        {
            var print_settings = GetPrinterSettings(cmdlet, printername);

            if (paper_source_cache == null)
            {
                cmdlet.WriteVerbose("Creating PaperSource Cache");
                paper_source_cache = new Dictionary<string, IList<System.Drawing.Printing.PaperSource>>();
            }

            if (printername ==null)
            {
                printername = print_settings.PrinterName;
            }

            if (paper_source_cache.ContainsKey(printername))
            {
                cmdlet.WriteVerbose("Found Cached Papersources");
                return paper_source_cache[printername];
            }
             

            cmdlet.WriteVerbose("Did not fined Cached Papersources");


            cmdlet.WriteVerbose("Getting sources");

            var list = new List<System.Drawing.Printing.PaperSource>(print_settings.PaperSources.Count);
            for (int i = 0; i < print_settings.PaperSources.Count; i++)
            {
                var source = print_settings.PaperSources[i];
                list.Add(source);
            }

            cmdlet.WriteVerbose("Adding to cache");
            paper_source_cache[printername] = list;

            return list;
        }


        public static IList<System.Drawing.Printing.PaperSize> GetPaperSizes(SMA.Cmdlet cmdlet, string printername)
        {
            var print_settings = GetPrinterSettings(cmdlet, printername);

            if (paper_size_cache == null)
            {
                cmdlet.WriteVerbose("Creating PaperSource Cache");
                paper_size_cache = new Dictionary<string, IList<System.Drawing.Printing.PaperSize>>();
            }

            if (printername == null)
            {
                printername = print_settings.PrinterName;
            }

            if (paper_size_cache.ContainsKey(printername))
            {
                cmdlet.WriteVerbose("Found Cached Paper sizes");
                return paper_size_cache[printername];
            }


            cmdlet.WriteVerbose("Did not fined Cached Papersizes");


            cmdlet.WriteVerbose("Getting sizes ");

            var list = new List<System.Drawing.Printing.PaperSize>(print_settings.PaperSizes.Count);
            for (int i = 0; i < print_settings.PaperSizes.Count; i++)
            {
                var size = print_settings.PaperSizes[i];
                list.Add(size);
            }

            cmdlet.WriteVerbose("Adding to cache");
            paper_size_cache[printername] = list;

            return list;
        }

    }

}